import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { RouteRevokeService } from './route-revoke.service';
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { SearchRemindComponent } from '../search-remind/search-remind.component';
import { DataService } from '../../../../../services/data.service';

@Component({
  selector: 'app-route-revoke',
  templateUrl: './route-revoke.component.html',
  styleUrls: ['./route-revoke.component.scss'],
  providers: [RouteRevokeService]
})
export class RouteRevokeComponent implements OnInit {

  //保存并提交的时候提交的提醒人数据
  public auditContentBean: any = {
    auditOpr: '',//提醒人
    title: '',//标题
    content: '',//内容
    remark: ''//备注
  };
  //短信提醒
  public sSMSFlag: any = false;
  //邮件提醒
  public sEmailFlag: any = false;
  // 提醒人展示字段
  public displayName: any;
  //接受在途模式数据
  public RoutepostBody: any;
  // 查询提醒人、群组modal
  @ViewChild("routeRevokeSearchRemind", { read: ViewContainerRef })
  routeRevokeSearchRemind: ViewContainerRef;
  searchRemindComponent: ComponentRef<SearchRemindComponent>;
  constructor(
    public routeRevokeService: RouteRevokeService,
    public resolver: ComponentFactoryResolver,
    public dataService: DataService
  ) { }

  ngOnInit() {
    // reminderEmitter
    this.dataService.reminderEmitter = new EventEmitter();
    this.dataService.reminderEmitter.subscribe((value) => {
      if (value) {
        console.log('value' + value);
        this.auditContentBean.auditOpr = _.concat(value.sMbrName, value.sUsername).join(',');
        this.displayName = _.concat(value.sMbrName, value.sRealname)
        window['$']('#routeRevokeSearchRemind').modal('hide');
      }
    })
    this.defaultValueApi();
  }
  //查询提醒人列表
  showSearchRemind() {
    // 判断组件是否已创建，如果创建先注销
    if (this.searchRemindComponent && this.searchRemindComponent.instance) {
      this.searchRemindComponent.destroy();
    }
    // 动态创建组件的实例
    const searchRemindComponent = this.resolver.resolveComponentFactory(SearchRemindComponent);
    this.searchRemindComponent = this.routeRevokeSearchRemind.createComponent(searchRemindComponent);
    window['$']('#routeRevokeSearchRemind').modal('show');
  }
  //保存撤销
  recall() {
    let that = this;
    let postBody = {
      auditContentBean: this.auditContentBean,
      pmsNqdiiJournalExtList: this.RoutepostBody
    }
    console.log(JSON.stringify(postBody))
    window['swal']({
      type: "warning",
      showCancelButton: true,
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      closeOnConfirm: false,
      showLoaderOnConfirm: true,
      title: "提示",
      text: "<div style='max-height: 200px;overflow-y: auto;'>" + "请确认是否撤销提交选中的信息" + "</div>",
      html: true
    },
      function (isConfirm) {
        if (isConfirm) {
          that.routeRevokeService.recall(postBody).subscribe(data => {
            if (data) {
              window['swal']("提示", "撤销信息成功", "success");
              that.dataService.nQDIIInputModelEmitter.emit('revokeModel');
              that.hideModal();
            }
          })
        }
      })
  }
  //关闭modal
  hideModal() {
    window['$']('#nQDIIRouteRevokeModal').modal('hide');
    window['$']('#nQDIIRMRevokeModal').modal('hide');
    window['$']('#viewDetailRevokeModal').modal('hide');
    this.unlock();
  }
  //解锁
  unlock() {
    let that = this;
    let unlockList: any = [];
    console.log(this.RoutepostBody)
    _.forEach(this.RoutepostBody, item => {
      unlockList.push(item.nId);
    })
    let getBody = {
      "nIdList": unlockList
    }
    this.routeRevokeService.unlock(getBody).subscribe(data => {
      if (data) {
        console.log(data)
        console.log("解锁成功")
      }
    })
  }
  //设置默认接口
  setDefault() {
    let that = this;
    let postBody = _.cloneDeep(this.auditContentBean);
    postBody['sButtonId'] = "70172@nqdii_enter1";
    postBody['sEmailFlag'] = _.cloneDeep(this.sEmailFlag);
    postBody['sEmailContent'] = _.cloneDeep(postBody.content);
    postBody['sSMSFlag'] = _.cloneDeep(this.sSMSFlag);
    postBody['sSMSContent'] = _.cloneDeep(postBody.content);
    delete postBody.content;

    if (postBody.sEmailFlag) {
      postBody.sEmailFlag = 'Y'
    } else {
      postBody.sEmailFlag = 'N'
    }
    if (postBody.sSMSFlag) {
      postBody.sSMSFlag = 'Y'
    } else {
      postBody.sSMSFlag = 'N'
    }
    console.log(postBody)
    window['swal']({
      type: "warning",
      showCancelButton: true,
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      closeOnConfirm: false,
      showLoaderOnConfirm: true,
      title: "提示",
      text: "<div style='max-height: 200px;overflow-y: auto;'>" + "请确认是否设置默认" + "</div>",
      html: true
    },
      function (isConfirm) {
        if (isConfirm) {
          that.routeRevokeService.setDefault(postBody).subscribe(data => {
            if (data) {
              window['swal']("提示", "设置默认成功", "success");
            }

          })
        }
      });
    console.log(postBody)
  }
  //获取默认模板接口
  defaultValueApi() {
    let that = this;
    let getBody: any = {
      "sButtonId": "70172@nqdii_enter1"
    }
    this.routeRevokeService.defaultValue(getBody).subscribe(data => {
      if (data) {
        console.log(data);
        if (Array.isArray(data)) {
          if (data.length > 1) {
            that.displayName = data[0].sContentZh;
            that.auditContentBean.auditOpr = data[0].sContent;
            if (data[0].sSMSFlag == 'true') {
              that.sSMSFlag = true;
            }
            if (data[0].sEmailFlag == 'true') {
              that.sEmailFlag = true;
            }
            that.auditContentBean.content = data[1].sContent;
            that.auditContentBean.title = data[1].sRemindTitle;
          }
        }
      }
    })
  }
}
